//
//  SubjectDetailPageVC.m
//  NewNest
//
//  Created by hesiyao on 16/1/18.
//  Copyright © 2016年 www.lanou3g.com蓝鸥科技. All rights reserved.
//

#import "SubjectDetailPageVC.h"
#import "SubjectDetailPageModal.h"
#import <AFHTTPSessionManager.h>
#import <AFNetworking.h>
#import "BaseView.h"
#import <UIImageView+WebCache.h>
@interface SubjectDetailPageVC ()
@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *contentLabel;
@property (nonatomic, retain) NSMutableDictionary *dataDic;
@property (nonatomic, retain) NSMutableArray *imgArr;
@property (nonatomic, retain) UIScrollView *scrollview;
@end

@implementation SubjectDetailPageVC

-(void)dealloc
{
    [_idStr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"专题详情";
    
    // 数据处理
    [self dataHandler];
    
    
    
}

///// 自适应高度方法
//+ (CGFloat)heightWith:(NSString *)text size:(CGSize)Size
//{
//    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:17]};
//    CGSize size = Size;
//    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
//    return rect.size.height;
//}

/// 视图创建
- (void)createSubviews
{
    self.scrollview = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:_scrollview];
    self.scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height / 2.5 * (_imgArr.count + 1));
//    self.automaticallyAdjustsScrollViewInsets = NO;
    [_scrollview release];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, self.view.frame.size.width - 20, self.view.frame.size.height / 20)];
    [self.scrollview addSubview:_titleLabel];
//    _titleLabel.backgroundColor = [UIColor yellowColor];
    _titleLabel.font = [UIFont systemFontOfSize:19];
    _titleLabel.textAlignment = NSTextAlignmentCenter;
    [_titleLabel release];
    
    self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 20 + _titleLabel.frame.size.height, self.view.frame.size.width - 20, self.view.frame.size.height / 3.5)];
    [self.scrollview addSubview:_contentLabel];
//    _contentLabel.backgroundColor = [UIColor grayColor];
    _contentLabel.font = [UIFont systemFontOfSize:15];
    _contentLabel.numberOfLines = 0;
    [_contentLabel release];
    
    // 右上角的喜欢按钮
    UIButton *likeBtton = [UIButton buttonWithType:UIButtonTypeCustom];
    likeBtton.frame = CGRectMake(0, 0, 25, 25);
    [likeBtton setImage:[UIImage imageNamed:@"kongaixin@2x"] forState:UIControlStateNormal];
    [likeBtton setImage:[UIImage imageNamed:@"hongaixin@2x"] forState:UIControlStateSelected];
    
    //给按钮添加点击事件
    [likeBtton addTarget:self action:@selector(likeActoin) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:likeBtton];
    
    
}

/// 处理数据
- (void)dataHandler
{
    NSString *urlStr = [NSString stringWithFormat:@"http://appapi.17house.com/xiaoguotuApi.php?action=albumDetail&id=%@", _idStr];
    AFHTTPSessionManager *session = [AFHTTPSessionManager manager];
    [session GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功");
        self.dataDic = responseObject[@"data"];
        
        // 图片数组
        self.imgArr = _dataDic[@"images"];
        
        // 视图创建
        [self createSubviews];
        
        _titleLabel.text = _dataDic[@"name"];
        _contentLabel.text = _dataDic[@"desc"];
        
        // 遍历数组
        for (NSInteger i = 0; i < _imgArr.count; i++) {
            // 创建字典
            NSDictionary *dic = _imgArr[i];
            //如果like_desc不为空,就创建UIView 如果为空就创建UIImageView
            if ([dic[@"like_desc"] isEqualToString:@""])  {
                UIImageView *imgview = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height / 2.5 * (i + 1), self.view.frame.size.width - 20, self.view.frame.size.height / 2.5)];
                [imgview sd_setImageWithURL:[NSURL URLWithString:dic[@"path"]]];
                [self.scrollview addSubview:imgview];
                imgview.backgroundColor = [UIColor cyanColor];
                [imgview release];
            }else{
                BaseView *view = [[BaseView alloc] initWithFrame:CGRectMake(10, self.view.frame.size.height / 2.5 * (i + 1), self.view.frame.size.width - 20, self.view.frame.size.height/ 2.5)];
                [view.mainImg sd_setImageWithURL:[NSURL URLWithString:dic[@"path"]]];
                view.titleLabel.text = dic[@"like_desc"];
                [self.scrollview addSubview:view];
                [view release];
            }
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"失败");
    }];
    
}

/// 右侧喜欢按钮方法实现
- (void)likeActoin
{
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
